package com.huawei.hwclouds.lubanops.demo.common.exception;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import com.huawei.hwclouds.lubanops.demo.common.exception.common.InternalErrorException;
import com.huawei.hwclouds.lubanops.demo.common.json.JsonUtil;

/**
 * 测试异常类的序列化和反序列化
 *
 * @author y00438022
 * @since 2020/8/16
 **/
public class ErrorCodeExceptionTest {

    @Test
    public void test() {
        Map<String, String> error = new HashMap<>();
        String errorCode = "apm2.00000001";

        error.put("error_code", errorCode);
        error.put("error_msg", "some message");
        error.put("hint", "apm2.00000001:some message:xxxxxxxxx");
        String json = JsonUtil.toJSONString(error);
        System.out.println(error);

        ErrorCodeException exception = ErrorCodeException.toErrorCodeException(json);
        Assert.assertEquals("0000", exception.getServiceCode());
        Assert.assertEquals("0001", exception.getErrorCode());

    }

    @Test
    public void testServiceCodeEqual() {
        InternalErrorException errorException = new InternalErrorException("deliberate exception");
        String json = errorException.toJsonString(true);
        System.out.println(json);

        ErrorCodeException exception = ErrorCodeException.toErrorCodeException(json);
        Assert.assertEquals(errorException.getServiceCode(), exception.getServiceCode());
    }

    @Test
    public void testCorrectResponse() {
        Map<String, String> error = new HashMap<>();
        String errorCode = "apm2.00000001";

        error.put("error", errorCode);
        error.put("msg", "some message");
        error.put("hint", "apm2.00000001:some message:xxxxxxxxx");
        String json = JsonUtil.toJSONString(error);
        ErrorCodeException exception = ErrorCodeException.toErrorCodeException(json);
        Assert.assertTrue(exception == null);
    }
}
